<template>
  <div class="wrapper">
    <el-dialog
     title="修改登录密码"
     width="600px"
     :visible.sync="modelValue"
    >
      <div v-loading="dialogLoading">
        <el-form ref="form" :model="formParam" :rules="formRules" label-suffix="：" label-width="120px">
          <el-form-item label="旧密码" prop="OldPassword">
            <el-input v-model="formParam.OldPassword" type="password" @keyup.enter.native="submit('form')" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="NewPassword">
            <el-input v-model="formParam.NewPassword"  type="password" @keyup.enter.native="submit('form')" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="重复新密码" prop="reNewPassword">
            <el-input v-model="formParam.reNewPassword" @keyup.enter.native="submit('form')" type="password" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit('form')">确认修改</el-button>
            <el-button @click="modelValue = false">放弃</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DialogMixin from '@/mixins/dialog'
export default {
  mixins: [DialogMixin],
  components: {},
  props: {
  },
  data () {
    const validateRePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.formParam.NewPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      formParam: {
        OldPassword: '',
        NewPassword: '',
        reNewPassword: ''
      },
      formRules: {
        OldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        NewPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        reNewPassword: [
          { required: true, message: '请重复输入新密码', trigger: 'blur' },
          { validator: validateRePass, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    modelValue (val) {
      if (val) {
      } else {
        this.formParam = {
          OldPassword: '',
          NewPassword: '',
          reNewPassword: ''
        }
        this.$refs['form'].resetFields()
      }
    }
  },
  computed: {
  },
  methods: {
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.UpdateAdminPassword()
        } else {
          this.$message.warning('请检查表单是否全部填写正确！')
        }
      })
    },
    async UpdateAdminPassword () {
      this.dialogLoading = true
      const result = await this.$http.put(`/Shop/UpdateAdminPassword`, this.formParam)
      this.dialogLoading = false
      if (result.Code === 100000) {
        this.$emit('updateSuccess')
        this.$message.success('修改登录密码成功')
        this.modelValue = false
      } else if (result.Code === 110003) {
        this.$message.error('旧密码错误')
      } else {
        this.$message.error(result.Msg)
      }
    }
  },
  created () {},
  mounted () {}
}
</script>
<style lang="less" scoped>
.wrapper{}
</style>
